<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<title>教学班级</title>
		<meta name="renderer" content="webkit">
		<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
		<link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />
		<link rel="stylesheet" href="/css/font.css">
		<link rel="stylesheet" href="/css/xadmin.css">
		<link rel="stylesheet" href="/css/myCustom.css">
	</head>

	<body>
		<div class="x-nav">
			<span class="layui-breadcrumb">
                <a href="#">班级管理</a>
                <a>
                    <cite>教学班级</cite></a>
            </span>
			<a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right" href="javascript:location.replace(location.href);"
			 title="刷新">
				<i class="layui-icon" style="line-height:30px">ဂ</i></a>
		</div>

		<div class="x-body" id="app">
			<div class="layui-row layui-col-space15">
				<div class="layui-col-md3">
					<div class="layui-card">
						<xblock>
							专业列表
						</xblock>
						<ul id="majorTree">
						  <li><a href="javascript:;" class="allMajor"><i class="layui-icon layui-tree-leaf"></i><cite>全部</cite></a></li>
						</ul>
					</div>
				</div>
				<div class="layui-col-md9">
					<span  class="x-right"  style="line-height:40px;float:left">共有数据：{{ pageInfo.total }} 条，当前{{ pageInfo.pageNum }} / {{ pageInfo.pages }}页  <span style="color:#d2d2d2"></span> </span>
					<div style="display:inline;float:right;width: 108px" class="layui-form" onclick="confirmChooseCourse()">
					   <input type="checkbox" id="openChooseCourse" title="开启选课">
					</div>
					<table class="layui-table">
						<thead>
                            <tr>
                                <th width="20">
                                    <div class="layui-unselect header layui-form-checkbox" lay-skin="primary"><i class="layui-icon">&#xe605;</i></div>
                                </th>
                                <th>课程名称</th>
                                <th width="60">班级人数</th>
                                <th width="100">所属专业</th>
                                <th>备注</th>
                                <th width="60">操作</th>
                        </thead>
                        <tbody>
                            <tr v-for="schClass in schClasss">
                                <td>
                                    <div class="layui-unselect layui-form-checkbox" lay-skin="primary" :data-id='schClass.id' :data-className="schClass.className" :data-sort="schClass.sort"><i class="layui-icon">&#xe605;</i></div>
                                </td>
                                <td>{{ schClass.className }}</td>
                                <td>{{ schClass.stuNumber }}</td>
                                <td>{{ schClass.majorNo }}</td>
                                <td>根据选修课自动生成</td>
                                <td class="td-manage">
                                    <button class="layui-btn layui-btn layui-btn-sm" v-on:click="open_classManager(schClass.className, schClass.id)"><i class="layui-icon">&#xe770;</i>班级管理</button>
                                </td>
                            </tr>
                        </tbody>
					</table>
					<div class="page" style="margin-top: 10px !important;">
				        <div>
				          <a class="prev pageTxt" href="javascript:void(0)">上一页</a>
				          <a class="first pageTxt" href="javascript:void(0)">首页</a>
				          <template v-for="pageNum in pageInfo.navigatepageNums">
				              <a class="num" href="javascript:void(0)" v-if="pageInfo.pageNum != pageNum" :data="pageNum">{{ pageNum }}</a>
				              <span class="current" v-else :data="pageNum">{{ pageNum }}</span>
				          </template>
				          <a class="last pageTxt" href="javascript:void(0)">尾页</a>
				          <a class="next pageTxt" href="javascript:void(0)">下一页</a>
				        </div>
				      </div>
				</div>
			</div>
		</div>
		
		<script type="text/javascript" src="/js/jquery.min.js"></script>
	    <script type="text/javascript" src="/js/xadmin.js"></script>
	    <script type="text/javascript" src="/lib/layui/layui.js" charset="utf-8"></script>
	    <script type="text/javascript" src="/js/vue.min.js"></script>
	    <script type="text/javascript" src="/js/jquery.form.js"></script>
		<script>
			var vm = new Vue({
	            el : '#app',
	            data : {
	                pageInfo:"",
	                // 一页的班级列表
	                schClasss:"",
	                // 当前选中的专业代码（代表全部）
	                majorNo:"0",
	                // 当前课程类型（2：专业选修）
	                courseType: 2,
	                // 当前选课的开启状态
	                courseState: 0
	            },
	            created : function() {
	                // 获取学校专业列表（一级节点为院系，二级为专业）
	                this.listMajorWithTree();
	                // 获取当前选课的开启状态
	                this.chooseCourseState();
	                // 获取班级列表(0:全部)
	                this.listSchClass("0", this.courseType, 1);
	            },
	            methods : {
	            	listMajorWithTree : function() {
	                    var vm = this;
	                    $.ajax({
	                        url : "/sys/class/teach/schOrgWithMajorTree",
	                        type : "get",
	                        success : function(result) {
	                            if (result.state) {
	                            	var data = result.data;
	                                var majorTree = JSON.parse(data.listSchOrgWithMajorTree);
	                                // 加载tree数据(放在其它地方会容易导致加载不出来tree数据)
	                                layui.use('tree', function() {
	                                    layui.tree({
	                                        elem: '#majorTree', //传入元素选择器
	                                        nodes: majorTree,
	                                        click: function(node) {
	                                            // node即为当前点击的节点数据
	                                            // 如果选中的是院系
	                                            if (node.majorNo == "0") {
	                                            	return;
	                                            }
	                                            // 如果不是重复点击
	                                        	if (vm.majorNo != node.majorNo) {
		                                        	vm.majorNo = node.majorNo;
		                                        	vm.courseType = 2;
		                                        	vm.listSchClass(vm.majorNo, vm.courseType, 1);
	                                        	}
	                                        }
	                                    });
	                                });
	                            } else {
	                                layui.use(['layer'], function(){
	                                	var layer = layui.layer
		                                layer.msg(""+result.msg, {icon: 5});
                                	});
	                            }
	                        }
	                    });
	                },
	                chooseCourseState : function () {
	                	var vm = this;
                        $.ajax({
                            url : "/sys/class/teach/chooseCourseState",
                            type : "get",
                            success : function(result) {
                                if (result.state) {
                                    vm.courseState = result.data;
                                    if (result.data == 0) {
                                        $("#openChooseCourse").attr("checked", false);
                                    } else if(result.data == 1) {
                                        $("#openChooseCourse").attr("checked", true);
                                    }
                                } else {
                                    layui.use(['layer'], function(){
                                        var layer = layui.layer
                                        layer.msg(""+result.msg, {icon: 5});
                                    });
                                }
                            }
                        });
	                },
	                listSchClass : function(majorNo, courseType, pageNum) {
	                	var vm = this;
                        $.ajax({
                            url : "/sys/class/teach/listSchClass",
                            data:{
                            	majorNo:majorNo,
                            	courseType: courseType,
                            	pageNum:pageNum
                            },
                            type : "get",
                            success : function(result) {
                                if (result.state) {
                                	vm.pageInfo = result.data;
                                	vm.schClasss = result.data.list;
                                } else {
                                	layui.use(['layer'], function(){
                                        var layer = layui.layer
                                        layer.msg(""+result.msg, {icon: 5});
                                    });
                                }
                            }
                        });
	                }
	            }
	        });
			$(function() {
				// 首次加载tree数据时,默认选中"全部"
				$("#majorTree li:first").addClass("selectColor");
				
			})
			
			// 开启选课
			function confirmChooseCourse() {
				var state = vm.courseState;
				if (state == 0) {
					state = 1;
				} else if (state == 1) {
					state = 0;
				}
                 $.ajax({
                     url : "/sys/class/teach/updateStatus",
                     type : "post",
                     data : {
                         state: state
                     },
                     success : function(result) {
                         if (result.state) {
	                      	  if (vm.courseState == 0) {
	                             layer.msg('选课已开启!', {icon:1});
	                             vm.courseState = 1;
	                      	  } else if (vm.courseState == 1) {
	                      		  layer.msg('选课已关闭!', {icon:1});
	                              vm.courseState = 0;
	                      	  }
                         } else {
                             layer.msg(""+result.msg, {icon: 5});
                         }
                     }
                 });
			}
			
			// 对选中的当前树节点加背景颜色
			$("body").on("mousedown", ".layui-tree li a", function() {
				if ($(this).hasClass("allMajor")) {
					$(".layui-tree li, li a").removeClass("selectColor");
					// 如果是“全部”，单独处理
					$(this).parent("li").addClass("selectColor");
					if (vm.majorNo != "0" || vm.courseType == 3) {
						// 获取全部的班级
						vm.listSchClass("0", 2, 1);
						vm.majorNo = "0";
						vm.courseType = 2;
					}
				} else if ($(this).hasClass("publicCourse")) {
					$(".layui-tree li, li a").removeClass("selectColor");
                    // 如果是“公共选修”，单独处理
                    $(this).parent("li").addClass("selectColor");
                    if (vm.courseType != 3) {
                        // 获取公共选修课程
                        vm.listSchClass("0", 3, 1);
                        vm.courseType = 3;
                    }
				} else {
					// 所选节点有children代表该节点为院系，不是专业
					var hasChildren = $(this).parent("li").find("ul").length != 0;
					if (!hasChildren) {
						$(".layui-tree li, li a").removeClass("selectColor");
						$(this).addClass("selectColor");
					}
				}
			})

			// 点击页码数，进行跳转
	        $(".page").delegate(".num", "click", function() {
	            var pageNum = $(this).attr("data");
                vm.listSchClass(vm.majorNo, vm.courseType, pageNum);
	        });
	        // 上一页
	        $(".page").delegate(".prev", "click", function() {
	            if (vm.pageInfo.hasPreviousPage) {
	            	vm.listSchClass(vm.majorNo, vm.courseType, vm.pageInfo.pageNum - 1);
	            }
	        });
	        // 下一页
	        $(".page").delegate(".next", "click", function() {
	            if (vm.pageInfo.hasNextPage) {
	            	vm.listSchClass(vm.majorNo, vm.courseType, vm.pageInfo.pageNum + 1);
	            }
	        });
	        // 首页
	        $(".page").delegate(".first", "click", function() {
	            if (!vm.pageInfo.isFirstPage) {
	            	vm.listSchClass(vm.majorNo, vm.courseType, 1);
	            }
	        });
	        // 尾页
	        $(".page").delegate(".last", "click", function() {
	            if (!vm.pageInfo.isLastPage) {
	            	vm.listSchClass(vm.majorNo, vm.courseType, vm.pageInfo.pages);
	            }
	        });
			// 打开班级管理页面
			function open_classManager(className, tchCourseId) {
				x_admin_show('班级管理['+className+']','/sys/class/teach/toTeachClass_student?tchCourseId=' + tchCourseId, 600, 430);
			}
			
		</script>
	</body>

</html>
